package com.jason.lightreader.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jason.lightreader.response.Code;
import com.jason.lightreader.response.Result;
import com.jason.lightreader.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//授权失败处理流程
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        Result result = Result.get(Code.FAILED,"请求被拒绝！");
        String resultJSONString = objectMapper.writeValueAsString(result);
        //处理异常
        WebUtil.renderString(response,resultJSONString);
    }
}
